home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / unzip51 / extract.c < prev    next >
C/C++ Source or Header  |  1992-11-04  |  34KB  |  915 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   extract.c
  4.  
  5.   This file contains the high-level routines ("driver routines") for extrac-
  6.   ting and testing zipfile members.  It calls the low-level routines in files
  7.   explode.c, inflate.c, unreduce.c and unshrink.c.
  8.  
  9.   ---------------------------------------------------------------------------*/
  10.  
  11.  
  12. #include "unzip.h"
  13. #include "crypt.h"
  14. #ifdef  MSWIN
  15. #  include "wizunzip.h"
  16. #  include "replace.h"
  17. #endif
  18.  
  19.  
  20. /************************************/
  21. /*  Extract Local Prototypes, etc.  */
  22. /************************************/
  23.  
  24. static int store_info __((void));
  25. static int extract_or_test_member __((void));
  26.  
  27. static uch *mem_i_buffer;
  28. static uch *mem_o_buffer;
  29. static ulg mem_i_size, mem_i_offset;
  30. static ulg mem_o_size, mem_o_offset;
  31.  
  32. static char *VersionMsg =
  33.   " skipping: %-22s  need %s compat. v%u.%u (can do v%u.%u)\n";
  34. static char *ComprMsg =
  35.   " skipping: %-22s  compression method %d\n";
  36. static char *FilNamMsg =
  37.   "%s:  bad filename length (%s)\n";
  38. static char *ExtFieldMsg =
  39.   "%s:  bad extra field length (%s)\n";
  40. static char *OffsetMsg =
  41.   "file #%d:  bad zipfile offset (%s)\n";
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /**************************************/
  48. /*  Function extract_or_test_files()  */
  49. /**************************************/
  50.  
  51. int extract_or_test_files()    /* return PK-type error code */
  52. {
  53.     uch *cd_inptr;
  54.     int cd_incnt, error, error_in_archive=PK_COOL;
  55.     int renamed, query, len, filnum=(-1), blknum=0;
  56.     int *fn_matched=NULL, *xn_matched=NULL;
  57.     ush i, j, members_remaining, num_skipped=0, num_bad_pwd=0;
  58.     longint cd_bufstart, bufstart, inbuf_offset, request;
  59.     min_info info[DIR_BLKSIZ];
  60.  
  61.  
  62. /*---------------------------------------------------------------------------
  63.     The basic idea of this function is as follows.  Since the central di-
  64.     rectory lies at the end of the zipfile and the member files lie at the
  65.     beginning or middle or wherever, it is not very desirable to simply
  66.     read a central directory entry, jump to the member and extract it, and
  67.     then jump back to the central directory.  In the case of a large zipfile
  68.     this would lead to a whole lot of disk-grinding, especially if each mem-
  69.     ber file is small.  Instead, we read from the central directory the per-
  70.     tinent information for a block of files, then go extract/test the whole
  71.     block.  Thus this routine contains two small(er) loops within a very
  72.     large outer loop:  the first of the small ones reads a block of files
  73.     from the central directory; the second extracts or tests each file; and
  74.     the outer one loops over blocks.  There's some file-pointer positioning
  75.     stuff in between, but that's about it.  Btw, it's because of this jump-
  76.     ing around that we can afford to be lenient if an error occurs in one of
  77.     the member files:  we should still be able to go find the other members,
  78.     since we know the offset of each from the beginning of the zipfile.
  79.  
  80.     Begin main loop over blocks of member files.  We know the entire central
  81.     directory is on this disk:  we would not have any of this information un-
  82.     less the end-of-central-directory record was on this disk, and we would
  83.     not have gotten to this routine unless this is also the disk on which
  84.     the central directory starts.  In practice, this had better be the ONLY
  85.     disk in the archive, but maybe someday we'll add multi-disk support.
  86.   ---------------------------------------------------------------------------*/
  87.  
  88.     pInfo = info;
  89.     members_remaining = ecrec.total_entries_central_dir;
  90.  
  91.     /* malloc space for check on unmatched filespecs (OK if one or both NULL) */
  92.     if (filespecs > 0  &&
  93.         (fn_matched=(int *)malloc(filespecs*sizeof(int))) != NULL)
  94.         for (i = 0;  i < filespecs;  ++i)
  95.             fn_matched[i] = FALSE;
  96.     if (xfilespecs > 0  &&
  97.         (xn_matched=(int *)malloc(xfilespecs*sizeof(int))) != NULL)
  98.         for (i = 0;  i < xfilespecs;  ++i)
  99.             xn_matched[i] = FALSE;
  100.  
  101.     while (members_remaining) {
  102.         j = 0;
  103.  
  104.         /*
  105.          * Loop through files in central directory, storing offsets, file
  106.          * attributes, and case-conversion flags until block size is reached.
  107.          */
  108.  
  109.         while (members_remaining && (j < DIR_BLKSIZ)) {
  110.             --members_remaining;
  111.             pInfo = &info[j];
  112.  
  113.             if (readbuf(sig, 4) <= 0) {
  114.                 error_in_archive = PK_EOF;
  115.                 members_remaining = 0;  /* ...so no more left to do */
  116.                 break;
  117.             }
  118.             if (strncmp(sig, central_hdr_sig, 4)) {  /* just to make sure */
  119.                 fprintf(stderr, CentSigMsg, j);  /* sig not found */
  120.                 fprintf(stderr, ReportMsg);   /* check binary transfers */
  121.                 error_in_archive = PK_BADERR;
  122.                 members_remaining = 0;  /* ...so no more left to do */
  123.                 break;
  124.             }
  125.             /* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag */
  126.             if ((error = process_cdir_file_hdr()) != PK_COOL) {
  127.                 error_in_archive = error;   /* only PK_EOF defined */
  128.                 members_remaining = 0;  /* ...so no more left to do */
  129.                 break;
  130.             }
  131.             if ((error = do_string(crec.filename_length,FILENAME)) != PK_COOL) {
  132.                 if (error > error_in_archive)
  133.                     error_in_archive = error;
  134.                 if (error > PK_WARN) {  /* fatal:  no more left to do */
  135.                     fprintf(stderr, FilNamMsg, filename, "central");
  136.                     members_remaining = 0;
  137.                     break;
  138.                 }
  139.             }
  140.             if ((error = do_string(crec.extra_field_length, EXTRA_FIELD)) != 0)
  141.             {
  142.                 if (error > error_in_archive)
  143.                     error_in_archive = error;
  144.                 if (error > PK_WARN) {  /* fatal */
  145.                     fprintf(stderr, ExtFieldMsg, filename, "central");
  146.                     members_remaining = 0;
  147.                     break;
  148.                 }
  149.             }
  150.             if ((error = do_string(crec.file_comment_length,SKIP)) != PK_COOL) {
  151.                 if (error > error_in_archive)
  152.                     error_in_archive = error;
  153.                 if (error > PK_WARN) {  /* fatal */
  154.                     fprintf(stderr, "\n%s:  bad file comment length\n",
  155.                             filename);
  156.                     members_remaining = 0;
  157.                     break;
  158.                 }
  159.             }
  160.             if (process_all_files) {
  161.                 if (store_info())
  162.                     ++j;  /* file is OK; info[] stored; continue with next */
  163.                 else
  164.                     ++num_skipped;
  165.             } else {
  166.                 int   do_this_file = FALSE;
  167.                 char  **pfn = pfnames-1;
  168.  
  169.                 while (*++pfn)
  170.                     if (match(filename, *pfn)) {
  171.                         do_this_file = TRUE;
  172.                         if (fn_matched)
  173.                             fn_matched[pfn-pfnames] = TRUE;
  174.                         break;       /* found match, so stop looping */
  175.                     }
  176.                 if (do_this_file) {  /* check if this is an excluded file */
  177.                     char  **pxn = pxnames-1;
  178.  
  179.                     while (*++pxn)
  180.                         if (match(filename, *pxn)) {
  181.                             do_this_file = FALSE;
  182.                             if (xn_matched)
  183.                                 xn_matched[pxn-pxnames] = TRUE;
  184.                             break;
  185.                         }
  186.                 }
  187.                 if (do_this_file)
  188.                     if (store_info())
  189.                         ++j;            /* file is OK */
  190.                     else
  191.                         ++num_skipped;  /* unsupp. compression or encryption */
  192.             } /* end if (process_all_files) */
  193.  
  194.  
  195.         } /* end while-loop (adding files to current block) */
  196.  
  197.         /* save position in central directory so can come back later */
  198.         cd_bufstart = cur_zipfile_bufstart;
  199.         cd_inptr = inptr;
  200.         cd_incnt = incnt;
  201.  
  202.     /*-----------------------------------------------------------------------
  203.         Second loop:  process files in current block, extracting or testing
  204.         each one.
  205.       -----------------------------------------------------------------------*/
  206.  
  207.         for (i = 0; i < j; ++i) {
  208.             filnum = i + blknum*DIR_BLKSIZ;
  209.             pInfo = &info[i];
  210.             /*
  211.              * if the target position is not within the current input buffer
  212.              * (either haven't yet read far enough, or (maybe) skipping back-
  213.              * ward) skip to the target position and reset readbuf().
  214.              */
  215.             /* LSEEK(pInfo->offset):  */
  216.             request = pInfo->offset + extra_bytes;
  217.             inbuf_offset = request % INBUFSIZ;
  218.             bufstart = request - inbuf_offset;
  219.  
  220.             if (request < 0) {
  221.                 fprintf(stderr, SeekMsg, ReportMsg);
  222.                 error_in_archive = PK_BADERR;
  223.                 continue;   /* but can still go on */
  224.             } else if (bufstart != cur_zipfile_bufstart) {
  225.                 cur_zipfile_bufstart = lseek(zipfd, bufstart, SEEK_SET);
  226.                 if ((incnt = read(zipfd,(char *)inbuf,INBUFSIZ)) <= 0) {
  227.                     fprintf(stderr, OffsetMsg, filnum, "lseek");
  228.                     error_in_archive = PK_BADERR;
  229.                     continue;   /* can still do next file */
  230.                 }
  231.                 inptr = inbuf + (int)inbuf_offset;
  232.                 incnt -= (int)inbuf_offset;
  233.             } else {
  234.                 incnt += (inptr-inbuf) - (int)inbuf_offset;
  235.                 inptr = inbuf + (int)inbuf_offset;
  236.             }
  237.  
  238.             /* should be in proper position now, so check for sig */
  239.             if (readbuf(sig, 4) <= 0) {  /* bad offset */
  240.                 fprintf(stderr, OffsetMsg, filnum, "EOF");
  241.                 error_in_archive = PK_BADERR;
  242.                 continue;   /* but can still try next one */
  243.             }
  244.             if (strncmp(sig, local_hdr_sig, 4)) {
  245.                 fprintf(stderr, OffsetMsg, filnum,
  246.                         "can't find local header sig");   /* bad offset */
  247.                 error_in_archive = PK_BADERR;
  248.                 continue;
  249.             }
  250.             if ((error = process_local_file_hdr()) != PK_COOL) {
  251.                 fprintf(stderr, "\nfile #%d:  bad local header\n", filnum);
  252.                 error_in_archive = error;   /* only PK_EOF defined */
  253.                 continue;   /* can still try next one */
  254.             }
  255.             if ((error = do_string(lrec.filename_length,FILENAME)) != PK_COOL) {
  256.                 if (error > error_in_archive)
  257.                     error_in_archive = error;
  258.                 if (error > PK_WARN) {
  259.                     fprintf(stderr, FilNamMsg, filename, "local");
  260.                     continue;   /* go on to next one */
  261.                 }
  262.             }
  263.             if (extra_field != (uch *)NULL) {
  264.                 free(extra_field);
  265.                 extra_field = (uch *)NULL;
  266.             }
  267.             if ((error = do_string(lrec.extra_field_length,EXTRA_FIELD)) != 0) {
  268.                 if (error > error_in_archive)
  269.                     error_in_archive = error;
  270.                 if (error > PK_WARN) {
  271.                     fprintf(stderr, ExtFieldMsg, filename, "local");
  272.                     continue;   /* go on */
  273.                 }
  274.             }
  275.  
  276.             /*
  277.              * just about to extract file:  if extracting to disk, check if
  278.              * already exists, and if so, take appropriate action according to
  279.              * fflag/uflag/overwrite_all/etc. (we couldn't do this in upper
  280.              * loop because we don't store the possibly renamed filename[] in
  281.              * info[])
  282.              */
  283.             if (!tflag && !cflag) {
  284.                 renamed = FALSE;   /* user hasn't renamed output file yet */
  285. #ifdef OS2
  286.                 longname = FALSE;  /* no long name has yet been stored */
  287. #endif
  288.  
  289. startover:
  290.                 query = FALSE;
  291. #ifdef MACOS
  292.                 macflag = (pInfo->hostnum == MAC_);
  293. #endif
  294.                 /* mapname can create dirs if not freshening or if renamed */
  295.                 if ((error = mapname(!fflag || renamed)) > PK_WARN) {
  296.                     if ((error > PK_ERR) && (error_in_archive < PK_ERR))
  297.                         error_in_archive = PK_ERR;
  298.                     continue;   /* go on to next file */
  299.                 }
  300.  
  301.                 switch (check_for_newer(filename)) {
  302.                     case DOES_NOT_EXIST:
  303.                         if (fflag && !renamed)  /* don't skip if just renamed */
  304.                             continue;   /* freshen (no new files):  skip */
  305.                         break;
  306.                     case EXISTS_AND_OLDER:
  307.                         if (overwrite_none)
  308.                             continue;   /* never overwrite:  skip file */
  309.                         if (!overwrite_all && !force_flag)
  310.                             query = TRUE;
  311.                         break;
  312.                     case EXISTS_AND_NEWER:             /* (or equal) */
  313.                         if (overwrite_none || (uflag && !renamed))
  314.                             continue;  /* skip if update/freshen & orig name */
  315.                         if (!overwrite_all && !force_flag)
  316.                             query = TRUE;
  317.                         break;
  318.                 }
  319.                 if (query) {
  320. #ifdef MSWIN
  321.                     FARPROC lpfnprocReplace;
  322.                     int ReplaceDlgRetVal;   /* replace dialog return value */
  323.  
  324.                     ShowCursor(FALSE);      /* turn off cursor */
  325.                     SetCursor(hSaveCursor); /* restore the cursor */
  326.                     lpfnprocReplace = MakeProcInstance(ReplaceProc, hInst);
  327.                     ReplaceDlgRetVal = DialogBoxParam(hInst, "Replace",
  328.                       hWndMain, lpfnprocReplace, (DWORD)(LPSTR)filename);
  329.                     FreeProcInstance(lpfnprocReplace);
  330.                     hSaveCursor = SetCursor(hHourGlass);
  331.                     ShowCursor(TRUE);
  332.                     switch (ReplaceDlgRetVal) {
  333.                         case IDM_REPLACE_RENAME:
  334.                             renamed = TRUE;
  335.                             goto startover;
  336.                         case IDM_REPLACE_YES:
  337.                             break;
  338.                         case IDM_REPLACE_ALL:
  339.                             overwrite_all = TRUE;
  340.                             overwrite_none = FALSE;  /* just to make sure */
  341.                             break;
  342.                         case IDM_REPLACE_NONE:
  343.                             overwrite_none = TRUE;
  344.                             overwrite_all = FALSE;  /* make sure */
  345.                             force_flag = FALSE;     /* ditto */
  346.                             /* FALL THROUGH, skip */
  347.                         case IDM_REPLACE_NO:
  348.                             continue;
  349.                     }
  350. #else /* !MSWIN */
  351. reprompt:
  352.                     fprintf(stderr,
  353.                       "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
  354.                       filename);
  355.                     FFLUSH(stderr);
  356.                     fgets(answerbuf, 9, stdin);
  357.                     switch (*answerbuf) {
  358.                         case 'A':   /* dangerous option:  force caps */
  359.                             overwrite_all = TRUE;
  360.                             overwrite_none = FALSE;  /* just to make sure */
  361.                             break;
  362.                         case 'r':
  363.                         case 'R':
  364.                             do {
  365.                                 fprintf(stderr, "new name: ");
  366.                                 FFLUSH(stderr);
  367.                                 fgets(filename, FILNAMSIZ, stdin);
  368.                                 /* usually get \n here:  better check for it */
  369.                                 len = strlen(filename);
  370.                                 if (filename[len-1] == '\n')
  371.                                     filename[--len] = 0;
  372.                             } while (len == 0);
  373.                             renamed = TRUE;
  374.                             goto startover;   /* sorry for a goto */
  375.                         case 'y':
  376.                         case 'Y':
  377.                             break;
  378.                         case 'N':
  379.                             overwrite_none = TRUE;
  380.                             overwrite_all = FALSE;  /* make sure */
  381.                             force_flag = FALSE;     /* ditto */
  382.                             /* FALL THROUGH, skip */
  383.                         case 'n':
  384.                             continue;   /* skip file */
  385.                         default:
  386.                             fprintf(stderr, "error:  invalid response [%c]\n",
  387.                               *answerbuf);   /* warn the user */
  388.                             goto reprompt;   /* why not another goto? */
  389.                     } /* end switch (*answerbuf) */
  390. #endif /* ?MSWIN */
  391.                 } /* end if (query) */
  392.             } /* end if (extracting to disk) */
  393.  
  394. #ifdef CRYPT
  395.             if (pInfo->encrypted && (error = decrypt_member()) != PK_COOL) {
  396.                 if (error == PK_MEM2) {
  397.                     if (error > error_in_archive)
  398.                         error_in_archive = error;
  399.                     fprintf(stderr,
  400.                       " skipping: %-22s  unable to get password\n", filename);
  401.                 } else {  /* (error == PK_WARN) */
  402.                     fprintf(stderr,
  403.                       " skipping: %-22s  incorrect password\n", filename);
  404.                     ++num_bad_pwd;
  405.                 }
  406.                 continue;   /* go on to next file */
  407.             }
  408. #endif /* CRYPT */
  409.             disk_full = 0;
  410.             if ((error = extract_or_test_member()) != PK_COOL) {
  411.                 if (error > error_in_archive)
  412.                     error_in_archive = error;       /* ...and keep going */
  413.                 if (disk_full > 1)
  414.                     return error_in_archive;        /* (unless disk full) */
  415.             }
  416.         } /* end for-loop (i:  files in current block) */
  417.  
  418.  
  419.         /*
  420.          * Jump back to where we were in the central directory, then go and do
  421.          * the next batch of files.
  422.          */
  423.  
  424.         cur_zipfile_bufstart = lseek(zipfd, cd_bufstart, SEEK_SET);
  425.         read(zipfd, (char *)inbuf, INBUFSIZ);  /* were there b4 ==> no error */
  426.         inptr = cd_inptr;
  427.         incnt = cd_incnt;
  428.         ++blknum;
  429.  
  430. #ifdef TEST
  431.         printf("\ncd_bufstart = %ld (%.8lXh)\n", cd_bufstart, cd_bufstart);
  432.         printf("cur_zipfile_bufstart = %ld (%.8lXh)\n", cur_zipfile_bufstart,
  433.           cur_zipfile_bufstart);
  434.         printf("inptr-inbuf = %d\n", inptr-inbuf);
  435.         printf("incnt = %d\n\n", incnt);
  436. #endif
  437.  
  438.     } /* end while-loop (blocks of files in central directory) */
  439.  
  440. /*---------------------------------------------------------------------------
  441.     Check for unmatched filespecs on command line and print warning if any
  442.     found.
  443.   ---------------------------------------------------------------------------*/
  444.  
  445.     if (fn_matched) {
  446.         for (i = 0;  i < filespecs;  ++i)
  447.             if (!fn_matched[i])
  448.                 fprintf(stderr, "caution: filename not matched:  %s\n",
  449.                   pfnames[i]);
  450.         free(fn_matched);
  451.     }
  452.     if (xn_matched) {
  453.         for (i = 0;  i < xfilespecs;  ++i)
  454.             if (!xn_matched[i])
  455.                 fprintf(stderr, "caution: excluded filename not matched:  %s\n",
  456.                   pxnames[i]);
  457.         free(xn_matched);
  458.     }
  459.  
  460. /*---------------------------------------------------------------------------
  461.     Double-check that we're back at the end-of-central-directory record, and
  462.     print quick summary of results, if we were just testing the archive.  We
  463.     send the summary to stdout so that people doing the testing in the back-
  464.     ground and redirecting to a file can just do a "tail" on the output file.
  465.   ---------------------------------------------------------------------------*/
  466.  
  467.     readbuf(sig, 4);
  468.     if (strncmp(sig, end_central_sig, 4)) {     /* just to make sure again */
  469.         fprintf(stderr, EndSigMsg);  /* didn't find end-of-central-dir sig */
  470.         fprintf(stderr, ReportMsg);  /* check binary transfers */
  471.         if (!error_in_archive)       /* don't overwrite stronger error */
  472.             error_in_archive = PK_WARN;
  473.     }
  474.     if (tflag) {
  475.         int num=filnum+1 - num_bad_pwd;
  476.  
  477.         if (quietflg == 1) {
  478.             if (error_in_archive)
  479.                 printf("At least one error was detected in %s.\n", zipfn);
  480.             else if (num == 0)
  481.                 printf("Caution:  zero files tested in %s.\n", zipfn);
  482.             else if (process_all_files && (num_skipped+num_bad_pwd == 0))
  483.                 printf("No errors detected in compressed data of %s.\n", zipfn);
  484.             else
  485.                 printf("No errors detected in %s for the %d file%s tested.\n",
  486.                   zipfn, num, (num==1)? "":"s");
  487.             if (num_skipped > 0)
  488.                 printf("%d file%s skipped because of unsupported compression \
  489. or encoding.\n", num_skipped, (num_skipped==1)? "":"s");
  490. #ifdef CRYPT
  491.             if (num_bad_pwd > 0)
  492.                 printf("%d file%s skipped because of incorrect password.\n",
  493.                   num_bad_pwd, (num_bad_pwd==1)? "":"s");
  494. #endif /* CRYPT */
  495.         } else if ((quietflg == 0) && !error_in_archive && (num == 0))
  496.             printf("Caution:  zero files tested in %s.\n", zipfn);
  497.     }
  498.  
  499.     /* give warning if files not tested or extracted */
  500.     if ((num_skipped > 0) && !error_in_archive)
  501.         error_in_archive = PK_WARN;
  502. #ifdef CRYPT
  503.     if ((num_bad_pwd > 0) && !error_in_archive)
  504.         error_in_archive = PK_WARN;
  505. #endif /* CRYPT */
  506.  
  507.     return error_in_archive;
  508.  
  509. } /* end function extract_or_test_files() */
  510.  
  511.  
  512.  
  513.  
  514.  
  515. /***************************/
  516. /*  Function store_info()  */
  517. /***************************/
  518.  
  519. static int store_info()   /* return 0 if skipping, 1 if OK */
  520. {
  521. #define UNKN_COMPR \
  522.    (crec.compression_method>IMPLODED && crec.compression_method!=DEFLATED)
  523.  
  524. /*---------------------------------------------------------------------------
  525.     Check central directory info for version/compatibility requirements.
  526.   ---------------------------------------------------------------------------*/
  527.  
  528.     pInfo->encrypted = crec.general_purpose_bit_flag & 1;    /* bit field */
  529.     pInfo->ExtLocHdr = (crec.general_purpose_bit_flag & 8) == 8;  /* bit */
  530.     pInfo->text = crec.internal_file_attributes & 1;         /* bit field */
  531.     pInfo->crc = crec.crc32;
  532.     pInfo->compr_size = crec.csize;
  533.  
  534.     if (crec.version_needed_to_extract[1] == VMS_) {
  535.         if (crec.version_needed_to_extract[0] > VMS_VERSION) {
  536.             fprintf(stderr, VersionMsg, filename, "VMS",
  537.               crec.version_needed_to_extract[0] / 10,
  538.               crec.version_needed_to_extract[0] % 10,
  539.               VMS_VERSION / 10, VMS_VERSION % 10);
  540.             return 0;
  541.         }
  542. #ifndef VMS   /* won't be able to use extra field, but still have data */
  543.         else if (!tflag && !force_flag) {  /* if forcing, extract regardless */
  544.             fprintf(stderr,
  545.               "\n%s:  stored in VMS format.  Extract anyway? (y/n) ",
  546.               filename);
  547.             FFLUSH(stderr);
  548.             fgets(answerbuf, 9, stdin);
  549.             if ((*answerbuf != 'y') && (*answerbuf != 'Y'))
  550.                 return 0;
  551.         }
  552. #endif /* !VMS */
  553.     /* usual file type:  don't need VMS to extract */
  554.     } else if (crec.version_needed_to_extract[0] > UNZIP_VERSION) {
  555.         fprintf(stderr, VersionMsg, filename, "PK",
  556.           crec.version_needed_to_extract[0] / 10,
  557.           crec.version_needed_to_extract[0] % 10,
  558.           UNZIP_VERSION / 10, UNZIP_VERSION % 10);
  559.         return 0;
  560.     }
  561.  
  562.     if UNKN_COMPR {
  563.         fprintf(stderr, ComprMsg, filename, crec.compression_method);
  564.         return 0;
  565.     }
  566. #ifndef CRYPT
  567.     if (pInfo->encrypted) {
  568.         fprintf(stderr, " skipping: %-22s  encrypted (not supported)\n",
  569.           filename);
  570.         return 0;
  571.     }
  572. #endif /* !CRYPT */
  573.  
  574.     /* map whatever file attributes we have into the local format */
  575.     mapattr();   /* GRR:  worry about return value later */
  576.  
  577.     pInfo->offset = (longint) crec.relative_offset_local_header;
  578.     return 1;
  579.  
  580. } /* end function store_info() */
  581.  
  582.  
  583.  
  584.  
  585.  
  586. #ifdef OLD_QQ
  587. #  define QCOND2   (quietflg < 2)
  588. #else
  589. #  define QCOND2   (!quietflg)
  590. #endif
  591.  
  592. /***************************************/
  593. /*  Function extract_or_test_member()  */
  594. /***************************************/
  595.  
  596. static int extract_or_test_member()    /* return PK-type error code */
  597. {
  598.     int error = PK_COOL;
  599.     ush b;
  600.     int r;
  601.  
  602.  
  603.  
  604. /*---------------------------------------------------------------------------
  605.     Initialize variables, buffers, etc.
  606.   ---------------------------------------------------------------------------*/
  607.  
  608.     bits_left = 0;
  609.     bitbuf = 0L;
  610.     outpos = 0L;
  611.     outcnt = 0;
  612.     outptr = outbuf;
  613.     zipeof = 0;
  614.     crc32val = 0xFFFFFFFFL;
  615.     newfile = TRUE;
  616.  
  617. #ifdef SYMLINKS
  618.     /* if file came from Unix and is a symbolic link and we are extracting
  619.      * to disk, prepare to restore the link */
  620.     if (S_ISLNK(pInfo->file_attr) && (pInfo->hostnum == UNIX_) && !tflag &&
  621.         !cflag && (lrec.ucsize > 0))
  622.         symlnk = TRUE;
  623.     else
  624.         symlnk = FALSE;
  625. #endif /* SYMLINKS */
  626.  
  627.     memset(outbuf, 0xAA, OUTBUFSIZ);
  628. #if (!defined(DOS_OS2)) || defined(MSWIN)
  629.     if (aflag)                  /* if we have a scratchpad, clear it out */
  630. #ifdef MSWIN
  631.         _fmemset(outout, 0xAA, OUTBUFSIZ);
  632. #else /* !MSWIN */
  633.         memset(outout, 0xAA, OUTBUFSIZ);
  634. #endif /* ?MSWIN */
  635. #endif /* !DOS_OS2 || MSWIN */
  636.  
  637.     if (tflag) {
  638.         if (!quietflg) {
  639.             fprintf(stdout, "  Testing: %-22s ", filename);
  640.             fflush(stdout);
  641.         }
  642.     } else {
  643.         if (cflag) {            /* output to stdout (or copy of it) */
  644.             outfd = dup(1);
  645. #ifdef DOS_OS2
  646.             if (!aflag)
  647.                 setmode(outfd, O_BINARY);
  648. #endif /* DOS_OS2 */
  649. #ifdef VMS
  650.             if (create_output_file())   /* VMS version required for stdout! */
  651.                 return PK_DISK;
  652. #endif
  653.         } else if (create_output_file())
  654.             return PK_DISK;
  655.  
  656.     } /* endif (!tflag) */
  657.  
  658. /*---------------------------------------------------------------------------
  659.     Unpack the file.
  660.   ---------------------------------------------------------------------------*/
  661.  
  662.     switch (lrec.compression_method) {
  663.  
  664.     case STORED:
  665.         if (!tflag && QCOND2) {
  666.             fprintf(stdout, " Extracting: %-22s ", filename);
  667.             if (cflag)
  668.                 fprintf(stdout, "\n");
  669.             fflush(stdout);
  670.         }
  671.         while (ReadByte(&b) && !disk_full)
  672.             OUTB(b)
  673.         break;
  674.  
  675.     case SHRUNK:
  676.         if (!tflag && QCOND2) {
  677.             fprintf(stdout, "UnShrinking: %-22s ", filename);
  678.             if (cflag)
  679.                 fprintf(stdout, "\n");
  680.             fflush(stdout);
  681.         }
  682.         unShrink();
  683.         break;
  684.  
  685.     case REDUCED1:
  686.     case REDUCED2:
  687.     case REDUCED3:
  688.     case REDUCED4:
  689.         if (!tflag && QCOND2) {
  690.             fprintf(stdout, " unReducing: %-22s ", filename);
  691.             if (cflag)
  692.                 fprintf(stdout, "\n");
  693.             fflush(stdout);
  694.         }
  695.         unReduce();
  696.         break;
  697.  
  698.     case IMPLODED:
  699.         if (!tflag && QCOND2) {
  700.             fprintf(stdout, "  Exploding: %-22s ", filename);
  701.             if (cflag)
  702.                 fprintf(stdout, "\n");
  703.             fflush(stdout);
  704.         }
  705.         if (((r = explode()) != 0) && (r != 5)) {   /* ignore 5 if seekable */
  706.             if ((tflag && quietflg) || (!tflag && !QCOND2))
  707.                 fprintf(stderr, "  error:  %s%s\n", r == 3?
  708.                   "not enough memory to explode " :
  709.                   "invalid compressed (imploded) data for ", filename);
  710.             else
  711.                 fprintf(stderr, "\n  error:  %s\n", r == 3?
  712.                   "not enough memory for explode operation" :
  713.                   "invalid compressed data for explode format");
  714.             error = (r == 3)? PK_MEM2 : PK_ERR;
  715.         }
  716.         break;
  717.  
  718.     case DEFLATED:
  719.         if (!tflag && QCOND2) {
  720.             fprintf(stdout, "  Inflating: %-22s ", filename);
  721.             if (cflag)
  722.                 fprintf(stdout, "\n");
  723.             fflush(stdout);
  724.         }
  725.         if ((r = inflate()) != 0) {
  726.             if ((tflag && quietflg) || (!tflag && !QCOND2))
  727.                 fprintf(stderr, "  error:  %s%s\n", r == 3?
  728.                   "not enough memory to inflate " :
  729.                   "invalid compressed (deflated) data for ", filename);
  730.             else
  731.                 fprintf(stderr, "\n  error:  %s\n", r == 3?
  732.                   "not enough memory for inflate operation" :
  733.                   "invalid compressed data for inflate format");
  734.             error = (r == 3)? PK_MEM2 : PK_ERR;
  735.         }
  736.         break;
  737.  
  738.     default:   /* should never get to this point */
  739.         fprintf(stderr, "%s:  unknown compression method\n", filename);
  740.         /* close and delete file before return? */
  741.         return PK_WARN;
  742.  
  743.     } /* end switch (compression method) */
  744.  
  745.     if (disk_full) {            /* set by FlushOutput()/OUTB() macro */
  746.         if (disk_full > 1)
  747.             return PK_DISK;
  748.         error = PK_WARN;
  749.     }
  750.  
  751. /*---------------------------------------------------------------------------
  752.     Write the last partial buffer, if any; set the file date and time; and
  753.     close the file (not necessarily in that order).  Then make sure CRC came
  754.     out OK and print result.
  755.   ---------------------------------------------------------------------------*/
  756.  
  757.     if (!disk_full && FlushOutput())
  758.         if (disk_full > 1)
  759.             return PK_DISK;
  760.         else {                  /* disk_full == 1 */
  761.             fprintf(stderr, "%s:  probably corrupt\n", filename);
  762.             error = PK_WARN;
  763.         }
  764.  
  765.     if (!tflag)
  766. #ifdef VMS
  767.         CloseOutputFile();
  768. #else /* !VMS */
  769. #ifdef MTS                      /* MTS can't set file time */
  770.         close(outfd);
  771. #else /* !MTS */
  772.         set_file_time_and_close();
  773. #endif /* ?MTS */
  774. #endif /* ?VMS */
  775.  
  776.     if (error > PK_WARN)  /* don't print redundant CRC error if error already */
  777.         return error;
  778.  
  779.     /* logical-AND crc32val for 64-bit machines */
  780.     if ((crc32val = ((~crc32val) & 0xFFFFFFFFL)) != lrec.crc32) {
  781.         /* if quiet enough, we haven't output the filename yet:  do it */
  782.         if ((tflag && quietflg) || (!tflag && !QCOND2))
  783.             fprintf(stderr, "%-22s ", filename);
  784.         fprintf(stderr, " bad CRC %08lx  (should be %08lx)\n", crc32val,
  785.                 lrec.crc32);
  786.         FFLUSH(stderr);
  787.         error = PK_WARN;
  788.     } else if (tflag) {
  789.         if (!quietflg)
  790.             fprintf(stdout, " OK\n");
  791.     } else {
  792.         if (QCOND2 && !error)
  793.             fprintf(stdout, "\n");
  794.     }
  795.  
  796.     return error;
  797.  
  798. } /* end function extract_or_test_member() */
  799.  
  800.  
  801.  
  802.  
  803.  
  804. /*******************************/
  805. /*  Function ReadMemoryByte()  */
  806. /*******************************/
  807.  
  808. int ReadMemoryByte(x)   /* return 8 if byte available, 0 if not */
  809.     ush *x;
  810. {
  811.     if (mem_i_offset < mem_i_size) {
  812.         *x = (ush)mem_i_buffer[(ush)mem_i_offset];
  813.         mem_i_offset++;
  814.         return 8;
  815.     } else
  816.         return 0;
  817. }
  818.  
  819.  
  820.  
  821.  
  822.  
  823. /****************************/
  824. /*  Function FlushMemory()  */
  825. /****************************/
  826.  
  827. int FlushMemory()   /* return PK-type error code */
  828. {
  829.     if (outcnt == 0)
  830.         return PK_COOL;
  831.  
  832.     if (mem_o_offset + outcnt <= mem_o_size) {
  833.         memcpy((char *)(mem_o_buffer+(ush)mem_o_offset), (char *)outbuf,
  834.           outcnt);
  835.         mem_o_offset += outcnt;
  836.         return PK_COOL;
  837.     } else
  838.         return PK_DISK;
  839. }
  840.  
  841.  
  842.  
  843.  
  844.  
  845. /***************************/
  846. /*  Function memextract()  */
  847. /***************************/
  848.  
  849. int memextract(tgt, tgtsize, src, srcsize)   /* extract compressed extra */
  850.     uch *tgt, *src;                          /*  field block; return 0 if */
  851.     ulg tgtsize, srcsize;                    /*  success, 1 if not */
  852. {
  853.     ush method, error=0;
  854.     ulg crc, oldcrc;
  855.     int r;
  856.  
  857.     method = makeword(src);
  858.     crc = makelong(src+2);
  859.  
  860.     mem_i_buffer = src + 2 + 4;      /* method and crc */
  861.     mem_i_size   = srcsize - 2 - 4;
  862.     mem_i_offset = 0;
  863.   
  864.     mem_o_buffer = tgt;
  865.     mem_o_size   = tgtsize;
  866.     mem_o_offset = 0;
  867.  
  868.     mem_mode = 1;
  869.  
  870.     bits_left = 0;
  871.     bitbuf = 0L;
  872.     outpos = 0L;
  873.     outcnt = 0;
  874.     outptr = outbuf;
  875.     zipeof = 0;
  876.  
  877.     switch (method) {
  878.         case STORED:
  879.             memcpy(tgt, src + 2 + 4, (extent) (srcsize - 2 - 4));
  880.             break;
  881.         case DEFLATED:        /* GRR:  change aflag/bflag temporarily? */
  882.             if ((r = inflate()) != 0) {
  883.                 fprintf(stderr, "error:  %s\n", r == 3 ?
  884.                   "not enough memory for inflate operation" :
  885.                   "invalid compressed data for the inflate format");
  886.                 error = (r == 3)? PK_MEM2 : PK_ERR;
  887.             }
  888.             FlushOutput();
  889.             break;
  890.         default:
  891.             fprintf(stderr,
  892.               "warning:  unsupported extra field compression type--skipping\n");
  893.             error = 1;   /* GRR:  this should be passed on up via SetEAs() */
  894.             break;
  895.     }
  896.  
  897.     mem_mode = 0;
  898.  
  899.     if (!error) {
  900.         oldcrc = crc32val;
  901.         crc32val = 0xFFFFFFFFL;
  902.         UpdateCRC((unsigned char *)mem_o_buffer, (int)mem_o_size);
  903.         crc32val = (~crc32val) & 0xFFFFFFFFL;
  904.  
  905.         if (crc32val != crc) {
  906.             printf("(Bad extra field CRC %08lx, should be %08lx)\n", crc32val,
  907.               crc);
  908.             error = 1;
  909.         }
  910.         crc32val = oldcrc; /* grrr ... this ugly kludge should be fixed */
  911.     }
  912.  
  913.     return error;
  914. }
  915.